<?php
class Maven_ManageCatalogSpr_Helper_Data extends Mage_Core_Helper_Data
{
    public function getFormattedDateTime($myDate)
	{
		
		$format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);		
		if(trim($myDate)!='')
		{
			$ar = explode(" ",$myDate);
			$dt = explode("-",$ar[0]);
			$tm = explode(":",$ar[1]);
			//$str = date('F j, Y, g:i a', mktime($tm[0], $tm[1], $tm[2], $dt[1], $dt[2], $dt[0]) );
			$str = Mage::app()->getLocale()->date($dt[0]."-".$dt[2]."-".$dt[1]." ".$ar[1])->toString($format);
				//$str .= Mage::getBlockSingleton('core/text')->formatDate($col['created_time'], $format);
				//$this->formatDate($col['created_time'],'long');
			return $str;
		}
		else return "";
	}
	public function getLastRunHtml($title = 'Price')
	{
		$col = Mage::getResourceModel('managecatalogspr/managecatalog')->getLast($title);
		//$countCol = Mage::getResourceModel('managecatalog/importlog');
		//m($countCol,0);
			
		$format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);

		
		$success 	= " - ";
		$fail 		= " - ";
		$total 		= " - ";
		$pending 	= " - ";
		$created_at = " - ";
		$updated_at = " - ";
		$titleDateFinish = "Date Finished";
		
		if(count($col)>0 && isset($col['total'])) {
			$success 	= $col['success'];
			$fail 		= $col['fail'];
			$total 		= $col['total'];
			$pending 	= $total - ($success + $fail);
			
		
			if($col['created_time']!='') {
				$created_at = $this->getFormattedDateTime($col['created_time']);
			}
			if($col['update_time']!='') {
				$updated_at = $this->getFormattedDateTime($col['update_time']);
			}
			if($col['status']==0) {
				$titleDateFinish = "Last Updated";
			}
		}
		
		$html = "<style type=\"text/css\">
				.lastrun_table th {padding-left:5px;}
				.lastrun_table td {padding-left:5px;}
				.lastrun_table tr.head {background-color: #B5D5DF; height: 20px; font-weight: bold;}
				.lastrun_table tr {background-color: #FFF; }
				.lastrun_table tr.odd {background-color: #EFFFEF; }
				.lastrun_table tr.total {background-color: #EAFFDF; font-weight: bold; }	
				</style>
	     <TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2 WIDTH=500 class=\"lastrun_table\">
	     	<TR class=\"head\">
	     		<TH>&nbsp;</TH>
	     		<TH>Status</TH>
	     		<TH>SKUs</TH>
	     		<TH>Date Started</TH>
	     		<TH>".$titleDateFinish."</TH>
	     	</TR>
	     	<TR class=\"odd\" >
	     		<TD><img src=\"".Mage::getDesign()->getSkinUrl('images/right.gif')."\" alt=\"Success\" title=\"Success\" ></TD>
	     		<TD>Success</TD>
	     		<TD>".$success."</TD>
	     		<TD>&nbsp;</TD>
	     		<TD>&nbsp;</TD>
	     	</TR>
	     	<TR>
	     		<TD><img src=\"".Mage::getDesign()->getSkinUrl('images/cross.gif')."\" alt=\"Success\" title=\"Success\" ></TD>
	     		<TD>Problems</TD>
	     		<TD>".$fail."</TD>
	     		<TD>&nbsp;</TD>
	     		<TD>&nbsp;</TD>
	     	</TR>
	     	<TR class=\"odd\" >
	     		<TD></TD>
	     		<TD>Pending</TD>
	     		<TD>".$pending."</TD>
	     		<TD>&nbsp;</TD>
	     		<TD>&nbsp;</TD>
	     	</TR>
	     	<TR class=\"total\">
	     		<TD colspan=2>Total</TD>
	     		<TD>".$total."</TD>
	     		<TD>".$created_at."</TD>
	     		<TD>".$updated_at."</TD>
	     	</TR>
	     </TABLE>
	     ";
		return $html;
	}
	public function getRunButton($title,$url)
	{
		$html='';
		switch($title) {
			case 'Price':
				$cols = Mage::getResourceModel('managecatalogspr/managecatalog')->getLast($title);
				$flag=0;
				if(isset($cols['status']) && $cols['status']==0){
					$flag = 1;
				}

				if($flag==0) {
					$html = "<input type=\"button\" id=\"PriceRun\" name=\"PriceRun\" value=\"Run Now\" onclick=\"javascript:submitRunPrice(this);\" />";
					$html .= "<SCRIPT>
						function submitRunPrice(ele) {
							//alert('under Construction!');
							//alert('".$url."');
							//return false;
							
							ele.disabled = true;
							ele.value='Running......';
							var url = '".$url."';
							window.open(url,'_blank');
						}
				</SCRIPT>";
				}else //if(($cols['status']==0 || $cols['update_time']==''))
				{
					$html = "<b>Price Import is Running....</b>";
				}
				break;
			case 'Stock':

				$cols = Mage::getResourceModel('managecatalogspr/managecatalog')->getLast($title);
				$flag=0;
				if(isset($cols['status']) && $cols['status']==0){
					$flag = 1;
				}
				if($flag==0) {
					$html = "<input type=\"button\" id=\"StockRun\" name=\"StockRun\" value=\"Run Now\" onclick=\"javascript:submitRunStock(this);\" />";
					$html .= "<SCRIPT>
					function submitRunStock(ele) {
						//alert('under Construction!');
						//alert('".$url."');
						//return false;
						
						ele.disabled = true;
						ele.value='Running......';
						var url = '".$url."';
						window.open(url,'_blank');					}
				</SCRIPT>";


				}else  {
					$html = "<b>Stock Import is Running....</b>";
				}
				break;
				
			 case 'QuickPrice':

				$cols = Mage::getResourceModel('managecatalogspr/managecatalog')->getLast($title);
				$flag=0;
				if(isset($cols['status']) && $cols['status']==0){
					$flag = 1;
				}
				if($flag==0) {
					$html = "<input type=\"button\" id=\"QuickpriceRun\" name=\"QuickpriceRun\" value=\"Run Now\" onclick=\"javascript:submitRunQuickPrice(this);\" />";
					$html .= "<SCRIPT>
					function submitRunQuickPrice(ele) {
						//alert('under Construction!');
						//alert('".$url."');
						//return false;
						
						ele.disabled = true;
						ele.value='Running......';
						var url = '".$url."';
						window.open(url,'_blank');					}
				</SCRIPT>";


				}else  {
					$html = "<b>Quick Price Import is Running....</b>";
				}
				break;  
                
            case 'SprStock':

				$cols = Mage::getResourceModel('managecatalogspr/managecatalog')->getLast($title);
               	$flag=0;
				if(isset($cols['status']) && $cols['status']==0){
					$flag = 1;
				}
				if($flag==0) {
					$html = "<input type=\"button\" id=\"SprStockRun\" name=\"SprStockRun\" value=\"Run Now\" onclick=\"javascript:submitRunSprStock(this);\" />";
					$html .= "<SCRIPT>
					function submitRunSprStock(ele) {
						ele.disabled = true;
						ele.value='Running......';
						var url = '".$url."';
						window.open(url,'_blank');					}
				</SCRIPT>";


				}else  {
					$html = "<b>SPR Stock Import is Running....</b>";
				}
				break;  
			
			default:
				$html = " Not Available ";
				break;
		}
		return $html;
	}
	
}